// 判断当前浏览器是否支持WebSocket
var ws = null;
if ('WebSocket' in window) {
  ws = new WebSocket("ws://127.0.0.1:8888/");
} else {
  console.log('当前浏览器不支持WebSocket!');
}
const socket = {}
// function addListener() {
//   socket.addEventListener('open', function (event) {
//     socket.send('Hello Server!');
//   });
//   // 监听消息
//   socket.addEventListener('message', function (event) {
//     console.log('Message from server ', event.data);
//   });
// }
//建立连接时回调
ws.onopen = function () {
  console.log("connection open...");
  // 发送文本消息
  ws.send("hello");
}
// 接收到消息时回调
ws.onmessage = function (e) {
  if (typeof e.data === "string") {
    // 字符串格式
    console.log(e.data);
  }
};

// 连接关闭回调
ws.onclose = function () {
  console.log("connection closed...");
};

// 连接错误回调
ws.onerror = function () {
  console.log("connection error...");
};

export default ws;

